package com.lzy.shops.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lzy.shops.bean.PageBean;
import com.lzy.shops.bean.User;
import com.lzy.shops.bean.UserAddress;
import com.lzy.shops.bean.UserAndAddress;
import com.lzy.shops.service.UserAndAddressService;
import com.lzy.shops.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userAddress")
public class UserAndAddressController {

    @Autowired
    private UserAndAddressService service;

    private ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/findByUser/{userId}")
    public String findByUser(@PathVariable("userId") Integer userId,
                             @RequestParam("pageNo") String pageNo,
                             @RequestParam("pageSize") String pageSize) throws JsonProcessingException {
        ResponseData<PageBean> responseData = null;
        PageBean<UserAddress> pageBean = new PageBean<UserAddress>();
        int pageno = 0;
        int pagesize = 0;
        try {
            pageno = Integer.parseInt(pageNo);
            pagesize = Integer.parseInt(pageSize);
            pageBean.setList(new ArrayList<>());
            UserAndAddress userAndAddress = service.findByUser(userId,(pageno-1)*pagesize,pagesize);
            if(userAndAddress!=null){
                Integer count = service.findByUserCount(userId);
                if(count!=0){
                    pageBean.setTotalRecords(count);
                    pageBean.setList(userAndAddress.getUserAddressList());
                }
            }
        } catch (Exception e) {
            pageBean.setList(new ArrayList<>());
            responseData = new ResponseData<PageBean>(false,pageBean,e.getMessage());
        }
        pageBean.setPageNo(pageno);
        pageBean.setPageSize(pagesize);
        if(responseData==null){
            responseData = new ResponseData<PageBean>(true,pageBean,"lists");
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @PostMapping("/addAddress")
    public String addAddress(@RequestBody Map<String,String> msps) throws JsonProcessingException {
        ResponseData<Object> responseData = null;
        String userId = msps.get("userId");
        String params = msps.get("data").replace("[","").replace("]","");
        UserAddress userAddress = JSONObject.parseObject(params, UserAddress.class);
        Boolean bool = service.insertUserAddress(Integer.parseInt(userId), userAddress);
        if(bool){
            responseData = new ResponseData<Object>(true,"新增成功");
        }else{
            responseData = new ResponseData<Object>(false,"数据异常");
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @DeleteMapping("/deleteAddress")
    public String deleteAddress(String addressId,String userId) throws JsonProcessingException {
        ResponseData<Object> responseData = null;
        try {
            int a = Integer.parseInt(addressId);
            int u = Integer.parseInt(userId);
            if(a!=0&&u!=0){
                Boolean bool = service.delUserAddress(a, u);
                if(bool){
                    responseData = new ResponseData<Object>(true,"删除成功");
                }else{
                    responseData = new ResponseData<Object>(false,"数据异常");
                }
            }
        } catch (NumberFormatException e) {
            responseData = new ResponseData<Object>(false,"数据异常");
        }
        return objectMapper.writeValueAsString(responseData);
    }
}
